package org.bluedream.core.module.controller.depItem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.entity.depItem.DepItem;
import org.bluedream.core.module.service.depItem.DepItemService;
import org.bluedream.core.module.sys.entity.Role;
import org.bluedream.core.module.sys.entity.User;
import org.bluedream.core.module.sys.service.UserService;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName depItemUserController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-07-15 17:25
 * @Version
 */
@Controller
@RequestMapping(value = "${adminPath}/depItem")
public class DepItemController extends BaseController {
    @Resource
    private DepItemService depItemService;

    @Resource
    private UserService userService;

    @ModelAttribute
    public DepItem get(String id){
        return depItemService.getById(id);
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(DepItem entity, Model model){
        model.addAttribute("depItem" , entity);
        return "module/depItem/depItemList";
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping("/form")
    public String form(DepItem entity, Model model){
        entity = depItemService.createNextNode(entity , "code" , "tree_sorts" , "tree_sort");
        model.addAttribute("depItem" , entity);
        return "module/depItem/depItemForm";
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping("/userForm")
    public String userForm(DepItem entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("depItem" , entity);
        model.addAttribute("user" , new User());
        return "module/depItem/depItemUserSelect";
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping("/findPage4DepItem")
    @ResponseBody
    public IPage<User> findPage4User(@RsaSecurityParameter User user , @RsaSecurityParameter String searchId){
        return userService.findPageChecked(user , searchId , "depItem");
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return depItemService.treeData(excludeCode , isShowCode);
    }

    @RequiresPermissions("depItem:view")
    @RequestMapping("/treeDataToForm")
    @ResponseBody
    public List<Map<String , Object>> treeDataToForm(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return depItemService.treeData(excludeCode , isShowCode);
    }

    /**
     * 创建下一节点
     * @param entity
     * @return
     */
    @RequiresPermissions("depItem:edit")
    @RequestMapping(value = "/createNextNodeToForm" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DepItem createNextNodeToForm(@RequestBody DepItem entity){
        return depItemService.createNextNode(entity , "code" , "tree_sorts" , "tree_sort");
    }

    @RequiresPermissions("depItem:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(DepItem entity){
        boolean flag = depItemService.save(entity);
        if (flag){
            return "部门信息保存成功";
        }
        return "部门信息保存失败";
    }
}
